package com.google.code.nb_ldap_explorer.ssl_certificate_exception.gui;

import java.awt.*;
import java.security.cert.X509Certificate;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class CertificateChainDisplay extends javax.swing.JPanel {

    private JPanel usermessage = null;
    private CertificateDisplay cd = new CertificateDisplay();
    private X509Certificate[] certificateChain = null;

    /**
     * Creates new form CertificateDisplay
     */
    public CertificateChainDisplay() {
        initComponents();
        commonPanel.add(cd);
    }

    public void setUserMessage(JPanel c) {
        if (c == null && usermessage != null) {
            tabPanel.remove(usermessage);
            usermessage = null;
        }
        if (c != null) {
            usermessage = c;
            usermessage.setBorder(new EmptyBorder(5,5,5,5));
            tabPanel.insertTab("Message", null, usermessage, null, 0);
        }
        tabPanel.setSelectedIndex(0);
        syncSizeToCertificateDetail();
    }

    public void setCertificateChain(X509Certificate[] certificatesChain) {
        this.certificateChain = certificatesChain;
        DefaultListModel dlm = new DefaultListModel();
        if (certificatesChain != null) {
            for (X509Certificate cert : certificatesChain) {
                dlm.addElement(cert.getSubjectX500Principal().toString());
            }
        }

        cd.setCertificate(certificatesChain[certificateChain.length - 1]);

        certificateChainDNs.setModel(dlm);
        updateCertificateDetail();

        syncSizeToCertificateDetail();
    }

    private void syncSizeToCertificateDetail() {
        if (usermessage != null) {
            usermessage.setPreferredSize(cd.getPreferredSize());
            usermessage.setMinimumSize(cd.getMinimumSize());
        }
        detailPane.setPreferredSize(cd.getPreferredSize());
        detailPane.setMinimumSize(cd.getMinimumSize());
    }

    private void updateCertificateDetail() {
        int idx = certificateChainDNs.getSelectedIndex();
        if (idx >= 0 && certificateChain != null && idx < certificateChain.length) {
            certificateDetail.setText(certificateChain[idx].toString());
        } else {
            certificateDetail.setText("");
        }
        certificateDetail.setCaretPosition(0);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        tabPanel = new javax.swing.JTabbedPane();
        commonPanel = new javax.swing.JPanel();
        detailPane = new javax.swing.JPanel();
        certificateHierarchyHeadingLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        certificateChainDNs = new javax.swing.JList();
        certificateLayoutHeadingLabel = new javax.swing.JLabel();
        certificateDetailScrollPanel = new javax.swing.JScrollPane();
        certificateDetail = new javax.swing.JTextPane();

        setLayout(new java.awt.BorderLayout());

        tabPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));

        commonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        commonPanel.setLayout(new java.awt.BorderLayout());
        tabPanel.addTab(org.openide.util.NbBundle.getMessage(CertificateChainDisplay.class, "CertificateChainDisplay.commonPanel.TabConstraints.tabTitle"), commonPanel); // NOI18N

        detailPane.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        detailPane.setLayout(new java.awt.GridBagLayout());

        certificateHierarchyHeadingLabel.setFont(certificateHierarchyHeadingLabel.getFont().deriveFont(certificateHierarchyHeadingLabel.getFont().getStyle() | java.awt.Font.BOLD));
        certificateHierarchyHeadingLabel.setText(org.openide.util.NbBundle.getMessage(CertificateChainDisplay.class, "CertificateChainDisplay.certificateHierarchyHeadingLabel.text")); // NOI18N
        certificateHierarchyHeadingLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        detailPane.add(certificateHierarchyHeadingLabel, gridBagConstraints);

        jScrollPane1.setMinimumSize(new java.awt.Dimension(75, 75));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(75, 75));

        certificateChainDNs.setFont(certificateChainDNs.getFont().deriveFont(certificateChainDNs.getFont().getStyle() | java.awt.Font.BOLD));
        certificateChainDNs.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        certificateChainDNs.setMinimumSize(new java.awt.Dimension(22, 22));
        certificateChainDNs.setPreferredSize(new java.awt.Dimension(22, 22));
        certificateChainDNs.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                certificateChainDNsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(certificateChainDNs);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        detailPane.add(jScrollPane1, gridBagConstraints);

        certificateLayoutHeadingLabel.setFont(certificateLayoutHeadingLabel.getFont().deriveFont(certificateLayoutHeadingLabel.getFont().getStyle() | java.awt.Font.BOLD));
        certificateLayoutHeadingLabel.setText(org.openide.util.NbBundle.getMessage(CertificateChainDisplay.class, "CertificateChainDisplay.certificateLayoutHeadingLabel.text")); // NOI18N
        certificateLayoutHeadingLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        detailPane.add(certificateLayoutHeadingLabel, gridBagConstraints);

        certificateDetailScrollPanel.setPreferredSize(new java.awt.Dimension(22, 22));
        certificateDetailScrollPanel.setViewportView(certificateDetail);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        detailPane.add(certificateDetailScrollPanel, gridBagConstraints);

        tabPanel.addTab(org.openide.util.NbBundle.getMessage(CertificateChainDisplay.class, "CertificateChainDisplay.detailPane.TabConstraints.tabTitle"), detailPane); // NOI18N

        add(tabPanel, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void certificateChainDNsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_certificateChainDNsValueChanged
        updateCertificateDetail();
    }//GEN-LAST:event_certificateChainDNsValueChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList certificateChainDNs;
    private javax.swing.JTextPane certificateDetail;
    private javax.swing.JScrollPane certificateDetailScrollPanel;
    private javax.swing.JLabel certificateHierarchyHeadingLabel;
    private javax.swing.JLabel certificateLayoutHeadingLabel;
    private javax.swing.JPanel commonPanel;
    private javax.swing.JPanel detailPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane tabPanel;
    // End of variables declaration//GEN-END:variables
}
